﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using Tuan.BLL;
using System.Text;

public partial class Shared_forgetpassword : System.Web.UI.Page
{
    public bool isSuccess = false;
    //public bool isMailExist = false;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected  void op_Click(object sender, EventArgs e)
    {
        Tuan.BLL.UserInfo tempUser = new Tuan.BLL.UserInfo();
        if (!tempUser.Exists(name.Value))
        {
            isMailFound.Visible = true;
        }
        else
        {
            isMailFound.Visible = false;
            bool Result = SendMail("http://www.baidu.com", "http://www.google.cn");
           // Pan.Common.MailSender.Send(name.Value, "5151TUAN 密码重置", body);
            Response.Redirect("~/Shared/sendmailresult.aspx?sendResult="+Result.ToString());
        }
    }

    private bool SendMail(string link1, string link2)
    {
        StringBuilder body = new StringBuilder();
        body.Append("<P>您好</P><P>您要求了重设您在 5151TUAN 网站的帐户密码。</P>");
        body.Append("<P>您可以点击下面的链接登陆，或者将此链接复制到浏览器地址栏：</P>");
        body.AppendFormat ("<P><A href={0}>http://5151tuan.com/user/reset/252/1270732525/a83fc58d61bf52ec55d8aae5450986d5</A></P>",link1);
        body.Append("<P>这是一个一次性登陆链接，只能使用一次。此链接24小时后失效，如果未使用并不会有任何作用。</P>");
        body.Append("<P>如超时，您还可再次申请重置密码服务：</P>");
        body.AppendFormat("<P><A href={0}>http://www.5151tuan.com/shopuser/password</A></P>",link2);
        body.Append("<P>&nbsp;</P>");
        body.Append("<P>&nbsp;</P>");
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["UserName"]);
        string Pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"];
        string SmtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];

        mail.To.Add(new MailAddress(name.Value));
        mail.Body = body.ToString();
        mail.Subject = "5151TUAN 密码重置";
        mail.IsBodyHtml = true;
        mail.Priority = System.Net.Mail.MailPriority.High;
        mail.SubjectEncoding = System.Text.Encoding.UTF8;

        SmtpClient client = new SmtpClient(SmtpServer);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        // client.EnableSsl = true;    
        client.Credentials = new System.Net.NetworkCredential(mail.From.ToString(), Pwd);
        client.Timeout = 60000;
        try
        {
            client.Send(mail);
            isSuccess = true;
            
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
            isSuccess = false;
          
        }
        return isSuccess;
    }
}
